﻿using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Text;
using System.Text.Json;
using System.Windows;
using Microsoft.Win32;
using SimpleTool.ActivityUnit.EchoControl;
using SimpleTool.SerialTool.SerialControl;
using SimpleTool.TransformComponent;
using static SimpleTool.Server.ActivityDetetorServer;
using static SimpleTool.Server.PowerPlantServer;

namespace SimpleTool.Server
{
    public interface IPowerPlantServer
    {
        public string Name();

        public void Instance(Action<SerialPort> ObjectCallBack);

        /// <summary>
        /// <para> ChangeSeek - 监控串口变化的回调，true 为插入 false 为拔出 </para>
        /// <para> ActionType - 1 为插入 0 为拔出 </para>
        /// <para> isOpen  true 打开监控  false 关闭监控 </para>
        /// <para>=======================================</para>
        /// <para>EventType = 1  //  设备配置已更改</para>
        /// <para>EventType = 2  //  设备已到达(插入)</para>
        /// <para>EventType = 3  //  设备已移除</para>
        /// <para>EventType = 4  //  设备即将删除(允许阻止)</para>
        /// <param name="Out"></param>
        /// <param name="type"></param>
        /// <param name="isOpen"></param>
        /// </summary>
        public void Moniter(Action<bool>? ChangeSeek, int ActionType = 1,  bool isOpen = false);

        /// <summary>
        /// 0-close 1-open 2-isOpen
        /// </summary>
        /// <param name="stat"></param>
        public (bool, string) Action(int stat = 1);

        public void BuffClear();

        public void Subscribe(bool isSubscribe = true);

        public void Write(ICollection<CommandSectionModel.CommandStruct> pushs);

        public bool Execute(string command);

        public void Read<T>(ReceiveBuffContent action);
    }

    public interface ISerialServer
    {
        public ObservableCollection<string> ReceiveSerialInfo(EmSerialInfoType type);

    }

    public interface IDataBaseServer
    {
        /// <summary>
        /// access db type
        /// </summary>
        /// <param name="dbkey"></param>
        /// <param name="CallBack"></param>
        public DB.DB_TYPE GetDBIdentify();

        public void InitLocalDB(SqliteConnectionConfig Configurate, Action<bool>? InitNotify);

        public void InitCloudDB(string address, string key, Action<bool>? InitNotify);

        public Supabase.Client? GetCloudDB();

        /// <summary>
        /// 0 - local, 1 - supabase
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public KeyValuePair<string, string> GetImportantInfo(bool cloud = true);

        public void SetImportantInfo(KeyValuePair<string, string> info, bool cloud = true);

        public bool AlterLocalDBkey(string key);

        public void TemporaryRelease(bool isCloud = true);

        /// <summary>
        /// access db type
        /// </summary>
        /// <param name="dbkey"></param>
        /// <param name="CallBack"></param>
        public Task<DB.DB_TYPE> CheckDBSurvival(string? dbkey = null);

        public Task<SqliteExceuteResult> LocalDBQueryFree(params string[] sqls);

        public Task<SqliteExceuteResult> LocalDBQuery(ICollection<string> sql);

        public Task<bool> CheckField<FiedType>(string table, string field, FiedType value);

        /// <summary>
        /// <para>Table = 表名</para>
        /// <para>feedback = 执行结果回调</para>
        /// <para>start = 执行代码或方法回调,</para>
        /// <para>uncork = 执行重试失败回调</para>
        /// <para>type = 数据库操作类型</para>
        /// <para>maxAbnormalCount = 最大重试次数</para>
        /// </summary>
        /// <typeparam name="Table">表明</typeparam>
        /// <param name="feedback">执行结果回调</param>
        /// <param name="start">执行代码或方法回调</param>
        /// <param name="uncork">执行重试失败回调</param>
        /// <param name="type">数据库操作类型</param>
        /// <param name="maxAbnormalCount">最大重试次数</param>
        /// <returns></returns>
        public Task CloudAnchorChannel<Table>(Action feedback, Action start, Action? uncork = null, string? type = null, int maxAbnormalCount = 3);

        public void Dispose();
    }

    public interface IDirFileServer
    {
        public delegate void ReceiveLine(string conent);

        public string GetSuffix(string file);

        /// <summary>
        /// just support utf8、unicode encoding
        /// </summary>
        /// <param name="file"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public Encoding EncodingSee(string file);

        public string? Read(string file, Encoding? encoding = null);

        public Task<string?> ReadAsync(string file, Encoding? encoding = null);

        public ICollection<string> ReadLine(string file, Encoding? encoding = null);

        public Task ReadLineAsync(string file, ReceiveLine? receiveLine, Encoding? encoding = null);

        public Task<ICollection<string>> ReadLineAsync(string file, Encoding? encoding = null);

        public Task WriteAsync(string file, string? contet, Encoding? encoding = null);

        public Task<ICollection<string>> GetLocalFilesAsync(LocalFileFilterOptions Options);

        public string GetSimpleName(string file);

        public string GetSlippyName(string file);

        public string? CreateDir(string dir);

        public string? CreateFile(string file);

        public Task<object?> GetAssembly(string? name = null);

        public bool IsFile(string file);

        public bool IsDir(string dir);

        public string GetDir(string file);

        public string RegistryAction(int type, RegistryKey regkey, string path, string name, string key, string value="");

        public Task<Dictionary<string, Dictionary<string, string>>> ReadIniAsync(string file);

        public Task WriteIniAsync(string file, string ownerKey, string key, object value);

        public Task WriteIniAsync(string file, string ownerKey, Dictionary<string, string>? updatesMap);

        public bool OpenFile(string path);
    }

    public interface IDenCryptoGraphicServer
    {
        public Task<string> EnCryptoGraphic(string content, string password);

        public Task<string> DeCryptoGraphic(string crypt,   string password);

        public string ENbase64(string enstring);

        public string DEbase64(string destring);
    }

    public interface IActivityDetetorServer
    {
        public Task<object?> AutoFindDim(OrderFilterCondition Condition);

        public Task<Supabase.Postgrest.Responses.ModeledResponse<WorkOrderUnfold>?> WorkOrderCloudDimMatch(OrderFilterCondition Condition);

        public Task<Dictionary<string, ICollection<object>>?> WorkOrderLocalDimMatch(OrderFilterCondition Condition);

        public OrderFilterCondition.FieldType GetWorkOrderFieldType(string name);

        public Supabase.Interfaces.ISupabaseTable<WorkOrderUnfold, Supabase.Realtime.RealtimeChannel>? GetCloudFilterQuery(ICollection<OrderFilterCondition.Condition> Conditions);

        public string GetLocalFilterQuerySql(ICollection<OrderFilterCondition.Condition> Conditions, int target = 0);

        public void ContentPopupToolUIShow(EnjoyContentPopue.InfoPakage Ifpakage);

        public void WorkorderPreviewUIShow(Action? Showed, Action<ICollection<string>>? DisposeEraseTraceCallBack);

        public void RegexpTestToolUIShow(RegexpTestTool.InfoPakage Ifpakage);

        public void BitwiseXorToolUIShow(string content);

        public void TestStatisticalViewUIShow();

        public Task ConfigTemplateCreator(JsonSerializerOptions SafetyReferenceJsonOptions);

        public void Independent(string name, FrameworkElement? element = null);

        public void SetDelegate(OpenUIElement Delegate);
    }
}
